/**
 * @author hejun
 * @description 定义router配置
 */
import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'
//注册路由
Vue.use(Router)

// import Login from '../views/login'

// import Home from '../views/home'

// import Common from '../views/common'
const Login = () => import('../views/login')
const Home = () => import('../views/home')
const Common = () => import('../views/common')

//实例化
const router = new Router({
    //在路由器中有两种模式:hash(哈希,地址后面加#,为默认),history
    mode: 'history',
    //做路由地址与页面组件相关联的配置
    routes: [
        { path: '/', component: Login, },
        { path: '/login', component: Login, },
        { path: '/home', component: Home, meta: { noLogin: true } },
        { path: '*', component: Common, }
        // 路由可以使用meta属性来实现固定参数传递，只能在定义路由的时候来进行参数赋值
    ],
})
const callback = function (to, from, next) {
    //跳转拦截
    let noLogin = to.meta.noLogin
    if (!noLogin) {
        next()
    } else {
        const token = store.getters['aishang/getAmount']
          
        if (token) {
            next()
        } else {
            next('/login')
        }
    }
}
router.beforeEach(callback)

export default router


